package org.example.ychdp.controller;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.example.ychdp.entity.Concert;
import org.example.ychdp.entity.Order;
import org.example.ychdp.entity.User;
import org.example.ychdp.service.ConcertService;
import org.example.ychdp.service.OrderService;
import org.example.ychdp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/order")
@Controller
public class adminOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ConcertService concertService;

    @RequestMapping(value = "/orderManage")
    public ModelAndView testUserManage(HttpSession session,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "") String nickname,
                                       @RequestParam(required = false, defaultValue = "-1") Integer number) {
        if (session.getAttribute("admin")==null){
            Map<String,Object> model = new HashMap<String,Object>();
            String errorMsg = "您未登录，请登录";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("admin/adminLogin",model);
            return modelAndView;
        }
        else {
            PageInfo<Order> orderList = orderService.searchOrder(page, nickname, number);
            Map<Integer, User> userMap = new HashMap<>();
            for (Order order : orderList.getList()) {
                Integer userId = order.getUserid();  // 获取订单中的用户id
                if (!userMap.containsKey(userId) && userId!= null) {  // 如果还没查询过该用户，且userid不为空
                    User user = userService.selectByPrimaryKey(userId);  // 根据userid查询用户，这里假设findUserById是查询用户的方法
                    userMap.put(userId, user);
                }
            }
            Map<Integer, Concert> concertMap = new HashMap<>();
            for (Order order : orderList.getList()) {
                Integer concertId = order.getConcertid();  // 获取订单中的演唱会id
                if (!concertMap.containsKey(concertId) && concertId!= null) {
                    Concert concert = concertService.selectByPrimaryKey(concertId);  // 根据concertid查询演唱会，这里假设selectByPrimaryKey是查询方法
                    concertMap.put(concertId, concert);
                }
            }
            Map<String,Object> model = new HashMap<String,Object>();
            model.put("orderL",orderList);
            model.put("activeMenuItem", "orderManage");
            model.put("userMap", userMap);  // 将用户信息的映射表也放入模型中，方便在视图层使用
            model.put("concertMap", concertMap);
            ModelAndView modelAndView = new ModelAndView("admin/Order/orderManage",model);
            return modelAndView;
        }

    }

    @RequestMapping(value = "/editOrder", method = RequestMethod.GET)
    public String editUser(@RequestParam("id") String id, Model model) {
        Order order = orderService.getOrderById(id);
        model.addAttribute("order", order);
        return "admin/Order/editOrder";
    }

    @RequestMapping(value = "/updateOrder", method = RequestMethod.POST)
    public String updateUser(Order order) {
        orderService.updateOrder(order);
        return "redirect:orderManage";
    }

}
